package com.nanosn.coffeemaker.server.urls;

import com.nanosn.coffeemaker.server.restlet.StaticDirectory;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Map;
import org.restlet.util.Template;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * @see <a href="http://bitworking.org/projects/URI-Templates/">URI Template specification</a>
 * @author selkhateeb
 */
public class Pattern {

    private String uriTemplate;
    private String clazz;
    private Map<String, Object> methodParams;
    private StaticDirectory directory;

    public Map<String, Object> getParameters() {
        return methodParams;
    }

    public Pattern(String patternTemplate, Class<?> clazz) {
        this.uriTemplate = patternTemplate;
        this.clazz = clazz.getName();
    }

    public Pattern(String patternTemplate, String className) {
        this.uriTemplate = patternTemplate;
        this.clazz = className;
    }

    public Pattern(String patternTemplate, StaticDirectory directory) {
        this.uriTemplate = patternTemplate;
        this.clazz = null;
        this.directory = directory;
    }

    public StaticDirectory getDirectory() {
        return directory;
    }

    public String getClassName() {
        return clazz;
    }

    public String getUriTemplate() {
        return uriTemplate;
    }

    protected void setClassName(String className) {
        this.clazz = className;
    }

    //Quick test
    public static void main(String[] args) {
        Pattern p = new Pattern("/Poll/{user}", "com.nanosn.polls.Views.index");
        System.out.println(p.uriTemplate);
        System.out.println(p.clazz);
//        System.out.println(p.method);


        Template t = new Template("/Poll/{user}/{fun}");
        Map m = new Hashtable();
        t.parse("/Poll/3342/ddd", m);
        Method mm = null;

        for (Object object : m.keySet()) {
            System.out.println(object + ":" + m.get(object));
        }

    }

    void setMethodParams(Map<String, Object> variables) {
        this.methodParams = variables;
    }

    void setPackageName(String packageName) {
        if (this.clazz == null) {
            return;
        }
        if (!this.clazz.contains(".")) {
            this.setClassName(packageName + "." + this.getClassName());
        }
    }
}
